package cn.xpg.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.xpg.entity.Repertory;
import cn.xpg.service.RepertoryService;
import cn.xpg.tools.PageSupport;

@Controller
public class RepertoryController {

	@Resource(name = "repertoryService")
	private RepertoryService repertoryService;

	// 分页查询库存信息
	@RequestMapping("/getRepertoryList.do")
	public String getRepertoryList(HttpServletRequest request) {
		String product_name = request.getParameter("product_name");
		String pageIndex = request.getParameter("pageIndex");
		int pageSize = 4;
		int currentPageNo = 1;
		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		int totalCount = 0;
		totalCount = repertoryService.getRepertoryCount(product_name);
		System.out.println("查询库存信息！！！！！！！！！！————>getRepertoryList");

		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();

		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Repertory> repertoryList = null;
		repertoryList = repertoryService.getRepertoryList(product_name, (currentPageNo - 1) * pageSize, pageSize);

		request.setAttribute("repertoryList", repertoryList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("product_name", product_name);
		return "/jsp/jinxiaocun/repertory";
	}

	// 根据店铺名查询库存
	@RequestMapping("getRepertoryListBySname.do")
	public String getRepertoryListBySname(HttpServletRequest request) {
		String store_name = request.getParameter("store_name");
		String pageIndex = request.getParameter("pageIndex");
		int pageSize = 4;
		int currentPageNo = 1;
		int totalCount = 0;
		totalCount = repertoryService.getRepertoryCountBySname(store_name);
		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}

		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();

		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
		List<Repertory> repertoryListBySname = null;
		repertoryListBySname = repertoryService.getRepertoryListBySname(store_name, (currentPageNo - 1) * pageSize,
				pageSize);
		for (Repertory repertory : repertoryListBySname) {
			System.out.println(repertory.getProduct_id()+"----"+totalCount);
		}
		System.out.println("查询各店铺库存！！！！！！！！！！————>getRepertoryListBySname");
		request.setAttribute("repertoryListBySname", repertoryListBySname);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("store_name", store_name);
		return "/jsp/jinxiaocun/repertory_other";
	}

	// 新增库存
	@RequestMapping("addRepertory.do")
	public String addRepertory(@Valid Repertory repertory) {
		System.out.println("新增库存信息！！！！！！！！！！————>addRepertory");
		if (repertoryService.addRepertory(repertory) > 0) {
			return "redirect:/getRepertoryList.do";
		} else {
			return "/jsp/jinxiaocun/repertory";
		}
	}

	// 获取库存Id
	@RequestMapping("getRepertoryId.do")
	public String getRepertoryId(HttpServletRequest request) {
		System.out.println("获取库存Id！！！！！！！！！！————>getRepertoryId");
		int repertory_id = Integer.parseInt(request.getParameter("repertory_id"));
		Repertory repertory = repertoryService.getRepertoryId(repertory_id);
		request.setAttribute("repertory", repertory);
		return "/jsp/jinxiaocun/repertory_update";
	}

	// 修改库存
	@RequestMapping("updateRepertory.do")
	public String updateRepertory(@Valid Repertory repertory, HttpServletRequest request) {
		System.out.println("修改库存信息！！！！！！！！！！————>updateRepertory");
		if (repertoryService.updateRepertory(repertory) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getRepertoryList.do";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("repertory", repertory);
			return "/jsp/jinxiaocun/repertory_update";
		}
	}

	// 删除库存
	@RequestMapping("deleteRepertory.do")
	public String deleteRepertory(HttpServletRequest request) {
		System.out.println("删除库存信息！！！！！！！！！！————>deleteRepertory");
		int repertory_id = Integer.valueOf(request.getParameter("repertory_id"));
		if (repertoryService.deleteRepertory(repertory_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getRepertoryList.do";
	}
}
